<?php
/**
 * Author:xsc
 * Time:2023/9/1 10:49
 */

namespace App\Repository;

use App\Models\SpecialAlbum;

class SpecialAlbumRepo extends BaseRepo
{

    public function getQuery($params): \Illuminate\Database\Eloquent\Builder
    {
        $query = SpecialAlbum::query();
        $query->select(['id','name','cover','desc','create_time']);
        if(!empty($params['merchants_id'])) {
            $query->where("merchants_id",$params['merchants_id']);
        }
        if(!empty($params['name'])) {
            $query->where("name",'like','%'.$params['name'].'%');
        }
        return $query->orderByDesc("id");
    }

    /**
     * 根据商户id获取专辑
     * @param int $merchants_id
     * @return array
     * Author:xsc
     * Time:2023/9/1 10:58
     */
    public function getAlbum(int $merchants_id)
    {
        $res = SpecialAlbum::query()->where('merchants_id', $merchants_id)->orderBy('id', 'asc')->first();
        return $res ? $res->toArray() : [];
    }

    /**
     * 根据商户id获取专辑
     * @param $id
     * Author:xsc
     * Time:2023/9/1 10:58
     */
    public function getAlbumById($id)
    {
        return SpecialAlbum::query()->find($id);
    }

    /**
     * 新增/更新专辑
     * @param array $data
     * @param int $id
     * @return int
     * Author:xsc
     * Time:2023/9/6 16:01
     */
    public function save(array $data, int $id = 0)
    {
        if ($id) {
            return SpecialAlbum::query()->where('id', $id)->update($data) ? $id : 0;
        } else {
            return SpecialAlbum::query()->insertGetId($data);
        }
    }

    /**
     * 更新专辑销量
     * @param int $id
     * @param int $num
     * @return int
     * Author:xsc
     * Time:2023/9/6 19:33
     */
    public function updateSellCount(int $id, int $num = 1)
    {
        return SpecialAlbum::query()->where('id', $id)->increment('sell_count', $num);
    }


}
